package siac.com.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import siac.com.configuracao.ConexaoEclipselink;
import siac.com.entity.FacConsulta;

public class FacConsultaDao {
	private EntityManagerFactory factory;
	private EntityManager manager;
	private FacConsulta consulta;
	private List<FacConsulta> listaFacConsulta;

	public FacConsultaDao() {
		factory = ConexaoEclipselink.getEntityManagerFactory();
	}

	private EntityManager getEntityManager() {
		return factory.createEntityManager();
	}

	public FacConsulta getFacConsulta() {
		if (this.consulta == null) {
			this.consulta = new FacConsulta();
		}
		return consulta;
	}

	public void setFacConsulta(FacConsulta consulta) {
		this.consulta = consulta;
	}

	public List<FacConsulta> getListaFacConsulta() {
		return listaFacConsulta;
	}

	public void setListaFacConsulta(List<FacConsulta> listaFacConsulta) {
		this.listaFacConsulta = listaFacConsulta;
	}

	public List<FacConsulta> findAll() {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery("FacConsulta.findAll");
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public FacConsulta findId(Object id) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager
					.createNamedQuery("FacConsulta.findById").setParameter(
							"idTabela", id);
			this.consulta = (FacConsulta) query.getSingleResult();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.consulta;
	}

	public List<FacConsulta> findByDataInicioOcorrencia(Object dataInicioOcorrencia) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacConsulta.findByDataInicioOcorrencia").setParameter("dataInicioOcorrencia", dataInicioOcorrencia);
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public List<FacConsulta> findByDataFimOcorrencia(Object dataFimOcorrencia) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacConsulta.findByDataFimOcorrencia").setParameter("dataFimOcorrencia", dataFimOcorrencia);
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public List<FacConsulta> findByDataPeriodo(Object dataInicioOcorrencia, Object dataFimOcorrencia) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery("FacConsulta.findByDataPeriodo");
			query.setParameter("dataInicioOcorrencia", dataInicioOcorrencia);
			query.setParameter("dataFimOcorrencia", dataFimOcorrencia);
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public List<FacConsulta> findStatus(Object status) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacConsulta.findByStatus").setParameter("status", status);
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public List<FacConsulta> findByAtendimento(Object atendimento) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacConsulta.findByAtendimento").setParameter("atendimento", atendimento);
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public List<FacConsulta> findByMedico(Object medico) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacConsulta.findByMedico").setParameter("medico", medico);
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public List<FacConsulta> findByProcedimento(Object procedimento) {
		try {
			manager = getEntityManager();
			Query query = (Query) manager.createNamedQuery(
					"FacConsulta.findByProcedimento").setParameter("procedimento", procedimento);
			this.setListaFacConsulta(query.getResultList());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return this.listaFacConsulta;
	}

	public String save(FacConsulta bean) {
		try {
			manager = getEntityManager();
			manager.getTransaction().begin();
			manager.persist(bean);
			manager.getTransaction().commit();
			return "sucesso";
		} catch (Exception ex) {
			// ex.printStackTrace();
			// /messageUtility.addMensage(ex.getMessage(), "N");
			return "erro";
		}
	}

	public String delete(FacConsulta bean) {
		try {
			this.setFacConsulta(bean);
			manager = getEntityManager();
			manager.getTransaction().begin();
			FacConsulta ob = manager.find(FacConsulta.class, getFacConsulta()
					.getId());
			manager.remove(ob);
			manager.getTransaction().commit();
			return "sucesso";
		} catch (Exception ex) {
			// ex.printStackTrace();
			// /messageUtility.addMensage(ex.getMessage(), "N");
			return "erro";
		}
	}

	public String update(FacConsulta bean) {
		try {
			manager = getEntityManager();
			manager.getTransaction().begin();
			manager.merge(bean);
			manager.getTransaction().commit();
			return "sucesso";
		} catch (Exception ex) {
			// ex.printStackTrace();
			// /messageUtility.addMensage(ex.getMessage(), "N");
			return "erro";
		}
	}
}
